{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import sdm as sdmlib\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "bits = 1000\n",
    "sample = 1000000\n",
    "radius = 451\n",
    "scanner_type = sdmlib.SDM_SCANNER_THREAD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def write_sequence(sdms, bs_list):\n",
    "    n = len(bs_list)\n",
    "    for k in range(1, n+1):\n",
    "        for i in range(n-k):\n",
    "            print('Writing {} -> {} into sdm {}'.format(i, i+k, k-1))\n",
    "            sdms[k-1].write(bs_list[i], bs_list[i+k])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "address_space = sdmlib.AddressSpace.init_random(bits, sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "bitstrings = {}\n",
    "for x in ['A', 'B', 'C', 'D', 'E', 'F']:\n",
    "    bitstrings[x] = sdmlib.Bitstring.init_random(bits)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_bs_sequence(seq):\n",
    "    v = []\n",
    "    for x in seq:\n",
    "        v.append(bitstrings[x])\n",
    "    return v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "counter1 = sdmlib.Counter.create_file('seq-c1', bits, sample)\n",
    "sdm1 = sdmlib.SDM(address_space, counter1, radius, scanner_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "counter2 = sdmlib.Counter.create_file('seq-c2', bits, sample)\n",
    "sdm2 = sdmlib.SDM(address_space, counter2, radius, scanner_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "counter3 = sdmlib.Counter.create_file('seq-c3', bits, sample)\n",
    "sdm3 = sdmlib.SDM(address_space, counter3, radius, scanner_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing 0 -> 1 into sdm 0\n",
      "Writing 1 -> 2 into sdm 0\n",
      "Writing 2 -> 3 into sdm 0\n",
      "Writing 0 -> 2 into sdm 1\n",
      "Writing 1 -> 3 into sdm 1\n",
      "Writing 0 -> 3 into sdm 2\n"
     ]
    }
   ],
   "source": [
    "write_sequence([sdm1, sdm2, sdm3], get_bs_sequence('ABCD'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing 0 -> 1 into sdm 0\n",
      "Writing 1 -> 2 into sdm 0\n",
      "Writing 2 -> 3 into sdm 0\n",
      "Writing 0 -> 2 into sdm 1\n",
      "Writing 1 -> 3 into sdm 1\n",
      "Writing 0 -> 3 into sdm 2\n"
     ]
    }
   ],
   "source": [
    "write_sequence([sdm1, sdm2, sdm3], get_bs_sequence('EBCF'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_sequence(sdms, bs_list):\n",
    "    n = len(bs_list)\n",
    "    counters = []\n",
    "    for i in range(n):\n",
    "        print('Reading {} from sdm {}'.format(i, n-i-1))\n",
    "        c = sdms[n-i-1].read_counter(bs_list[i])\n",
    "        counters.append(c)\n",
    "    final = sdmlib.Counter.init_zero(bits, 1)\n",
    "    for c in counters:\n",
    "        final.add_counter(0, c, 0)\n",
    "    bs_final = final.to_bitstring(0)\n",
    "    return bs_final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_distances(ref):\n",
    "    v = [(x.distance_to(ref), key) for key, x in bitstrings.iteritems()]\n",
    "    v.sort(key=lambda x: x[0])\n",
    "    return v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading 0 from sdm 2\n",
      "Reading 1 from sdm 1\n",
      "Reading 2 from sdm 0\n"
     ]
    }
   ],
   "source": [
    "bs_final = read_sequence([sdm1, sdm2, sdm3], get_bs_sequence('ABC'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0 D\n",
      " 482 B\n",
      " 488 F\n",
      " 496 A\n",
      " 515 C\n",
      " 538 E\n"
     ]
    }
   ],
   "source": [
    "for d, x in calculate_distances(bs_final):\n",
    "    print('{:4d} {}'.format(d, x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading 0 from sdm 2\n",
      "Reading 1 from sdm 1\n",
      "Reading 2 from sdm 0\n"
     ]
    }
   ],
   "source": [
    "bs_final2 = read_sequence([sdm1, sdm2, sdm3], get_bs_sequence('EBC'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0 F\n",
      " 472 B\n",
      " 488 D\n",
      " 502 A\n",
      " 503 C\n",
      " 520 E\n"
     ]
    }
   ],
   "source": [
    "for d, x in calculate_distances(bs_final2):\n",
    "    print('{:4d} {}'.format(d, x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
